package com.gongpingjialoan.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.loans.LoanIndexActivity;
import com.gongpingjialoan.data.UserManager;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.utility.SharedPreferencesUtil;

/**
 * Created by Administrator on 2016/6/24.
 */
public class GuideActivity extends BaseActivity {

    private final int LOGIN = 1199;
    private ViewPager mViewPager;

    private int[] layouts = {R.layout.guide_layout1, R.layout.guide_layout2,
            R.layout.guide_layout3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSwipeEnagle();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.guide_layout);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(new MyAdapter());
    }

    private class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return layouts.length;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {

            View view = LayoutInflater.from(GuideActivity.this).inflate(layouts[position], null);
            if (position == layouts.length - 1) {
                ImageView imageView = (ImageView) view.findViewById(R.id.go);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        UserManager mUserManager = new UserManager(GuideActivity.this);
                        mUserManager = mUserManager.LoadUserInfo();
                        Intent it;
                        if (TextUtils.isEmpty(mUserManager.getToken()) || "failure".equals(GPJApplication.getInstance().checkLoginStatus)) {

                            it = new Intent(GuideActivity.this, LoginActivity.class);
                            it.putExtra("show_back", false);
                            startActivityForResult(it, LOGIN);

                        } else {
                            it = new Intent(GuideActivity.this, LoanIndexActivity.class);
                            startActivity(it);
                            finish();
                        }

//                        SharedPreferencesUtil.put(GuideActivity.this, "version_code" , GPJApplication.getInstance().verison_code);
                        SharedPreferencesUtil.put(GuideActivity.this, "ishow", true);
                    }
                });
            }
            container.addView(view, 0);
            return view;
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            if (requestCode == LOGIN) {
                Intent it = new Intent(GuideActivity.this, LoanIndexActivity.class);
                startActivity(it);
                finish();
            }


        }
    }
}
